import { GameRes } from "../GameRes";
import { ChangePageBase } from "./ChangePage.generated";
import { PageBtn } from "./PageBtn";

const { regClass } = Laya;

@regClass()
export class ChangePage extends ChangePageBase {
    onChangePage(totalPage: number, currentPage: number) {
        if (this.PageBox.numChildren == 0) {
            if (totalPage <= 7) {
                this.size(90 * (totalPage + 2), 72);
                this.space = 18;
                this.PageBox.space = 18;
                this.PageBox.size((90 * totalPage) - this.PageBox.space, 72);
                for (let i: number = 1; i <= totalPage; i++) {
                    this.createPrefab(i.toString())
                }
            }
            else {
                this.size(810, 72);
                this.space = 18;
                this.PageBox.space = 18;
                this.PageBox.size(630 - this.PageBox.space, 72);
                for (let i: number = 1; i <= 7; i++) {
                    this.createPrefab(i.toString())
                }
            }
        }
        else {
            if (currentPage >7) {
                for (let i: number = currentPage - 6; i <= currentPage; i++) {
                    let obj = this.PageBox.getChildAt(i - (currentPage - 6)).getChildByName("BG").getChildByName("Label") as Laya.Label;
                    obj.text = i.toString();
                }
            }
            else {
                for (let i: number = 1; i <= currentPage; i++) {
                    let obj = this.PageBox.getChildAt(i - 1).getChildByName("BG").getChildByName("Label") as Laya.Label;
                    obj.text = i.toString();
                }
            }

        }
        for (let i = 1; i <= totalPage; i++) {
            let obj = this.PageBox.getChildAt(i - 1) as PageBtn
            if (currentPage.toString() == obj?.Label.text) {
                obj?.Select();
            }
            else {
                obj?.Cancel();
            }
        }

    }
    createPrefab(str: string) {
        let prefab = GameRes.getResPrefab(GameRes.PageBtn) as Laya.Prefab;
        let obj = prefab.create() as PageBtn;
        obj.init(str);
        this.PageBox.addChild(obj);
    }
}